﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Entities.Users;
using DotNetNuke.UI.Skins.Controls;
using ShopSpace.Core;
using ShopSpace.Core.Repositories;

namespace ShopSpace.UI.DotNetNukeModules.ProjectManagement
{
	public partial class EditProjectPerson : EditModuleControlBase
	{
		private Project project;

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			try
			{
				this.InitParameters();
			}
			catch ( Exception ex )
			{
				this.HandleException(ex);
			}
		}
		
		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				this.SetupLinks();
				if ( !this.Page.IsPostBack )
				{
					this.SetupForm();
					this.BindProjectPerson();
				}
			}
			catch ( Exception ex )
			{
				this.HandleException(ex);
			}
		}

		protected void lnkUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				this.SaveProjectPerson();
				Response.Redirect(this.hlCancel.NavigateUrl);
			}
			catch ( Exception ex )
			{
				this.HandleException(ex);
			}
		}

		private void SetupForm()
		{
			if ( !this.editItemId.HasValue )
			{
				this.ddlPersons.DataSource = this.PersonRepository.ListAvailableForProject(this.project);
				this.ddlPersons.DataBind();
				this.ddlPersons.Items.Insert(0, new ListItem("Select One", "0"));
			}
			
			this.ddlRoles.Items.Add(new ListItem("Select One", "-1"));
			foreach ( string name in Enum.GetNames(typeof(Role)) )
			{
				this.ddlRoles.Items.Add(name);
			}
		}

		private void BindProjectPerson()
		{
			if ( this.editItemId.HasValue )
			{
				ProjectPerson projectPerson = this.ProjectPersonRepository.Get(this.editItemId.Value);
				this.ItemConcurrencyDate = projectPerson.ConcurrencyDate;
				this.ddlPersons.Items.Add(projectPerson.Person.DisplayName);
				this.ddlPersons.Enabled = false;
				this.ddlRoles.SelectedValue = projectPerson.Role.ToString();
			}
		}

		private void SaveProjectPerson()
		{
			Role selectedRole = (Role)Enum.Parse(typeof(Role), this.ddlRoles.SelectedValue);
			string message;
			if ( this.editItemId.HasValue )
			{
				ProjectPerson projectPerson = this.ProjectPersonRepository.Get(this.editItemId.Value);
				projectPerson.ConcurrencyDate = this.ItemConcurrencyDate;
				projectPerson.Role = selectedRole;
				this.ProjectPersonRepository.Update(projectPerson, this.UserInfo.Username);
				message = string.Format("{0} was updated", projectPerson.Person.DisplayName);
			}
			else
			{
				Person selectedPerson = this.PersonRepository.Get(int.Parse(this.ddlPersons.SelectedValue));
				ProjectPerson projectPerson = new ProjectPerson(this.project, selectedPerson, selectedRole);
				this.ProjectPersonRepository.Add(projectPerson, this.UserInfo.Username);
				message = string.Format("{0} was added", projectPerson.Person.DisplayName);
			}
			this.UserState.AddMessage(this.ModuleId, message, ModuleMessage.ModuleMessageType.GreenSuccess);
		}

		private void InitParameters()
		{
			this.project = this.ProjectRepository.Get(int.Parse(Request["ProjectId"]));
		}

		private void SetupLinks()
		{
			this.hlCancel.NavigateUrl = EditUrl("Id", this.project.Id.ToString(), "ViewProjectPersons");
		}
	}
}